<?php
namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;

class Advertuser extends Base
{
    /**
     * 用户审核列表
     * @return mixed
     */
    public function index()
    {
        $map = [
            'a.status' => 1,
        ];
        if(request()->isPost()){
           $data = input('post.');
           //如果code等于1则表明是搜索 如果为2则表明是搜索
           if($data['code'] == 1){

               $name = $data['username'];
               $map = [
                   'a.nick_name' => array('like',"%".$name."%"),
               ];

           }elseif($data['code'] == 2){

               //如果状态没有传过来 默认为1
               if($data['pass']){
                   $map['a.status'] = $data['pass'];
               }

               //时间区间
                if($data['start'] && $data['end']){
                    //把时间转换为uninx时间
                    $start_time = strtotime($data['start']);
                    $end_time = strtotime($data['end']);
                    $map['a.register_time'] = ['between',[$start_time,$end_time]];
                }

                //如果没有选择行业则默认为空
               if($data['industry']){
                   $map['a.job_one'] = $data['industry'];
               }

               //如果没有选择省份、城市则默认为空
               if($data['province'] || $data['city']){
                    $map['a.province'] = $data['province'];
                    $map['a.city'] = $data['city'];
               }
           }

        }

        //用户信息查询
        $user_info = Db::field('a.id as aid,a.uid,a.nick_name,a.id_card,a.register_time,a.province,a.city,a.job_one,a.status,b.provinceid,b.province,cityid,c.city,c.provinceid,d.id as did,d.name,e.id,e.user_name')
            ->table('ad_user_info')
            ->alias('a')
            ->join('ad_provinces b','a.province = b.provinceid','LEFT')
            ->join('ad_cities c','a.city = c.cityid','LEFT')
            ->join('ad_industry d','a.job_one = d.id','LEFT')
            ->join('ad_user e','a.uid = e.id','LEFT')
            ->where($map)
            ->paginate(10);

        //省份搜索数据
        $province = Db::table('ad_provinces')->select();

        //行业选择
        $industry = Db::field('id,name')->table('ad_industry')->where('pid = 0')->select();
        return $this->fetch('index',[
            'user_info' => $user_info,
            'province' => $province,
            'industry' => $industry,
        ]);
    }

    /**
     * 点击查看查询用户的详细信息
     * @return mixed
     */
    public function update()
    {
        $id = input("get.id");

        $more_info = Db::field('a.id as aid,a.uid,a.nick_name,a.province,a.city,a.job_one,a.id_card,b.provinceid,b.province,c.cityid,c.city,d.id,d.user_name,e.id,e.name')
                        ->table('ad_user_info')
                        ->alias('a')
                        ->join('ad_provinces b','a.province = b.provinceid')
                        ->join('ad_cities c','a.city = c.cityid')
                        ->join('ad_user d','a.uid = d.id')
                        ->join('ad_industry e','a.job_one = e.id')
                        ->where('a.id ='.$id )
                        ->find();


        return $this->fetch('update',[
            'more_info' => $more_info,
        ]);
    }

    //ajax获取城市列表
    public function getCity()
    {
        $provinceid = input("post.id");
        $city = model('cities')->getCityList($provinceid);
        return $city;
    }

    //ajax更改状态
    public function change(){
        //获取前端的id
        $status = input("post.");
        //进行数据更改
        $sta =  [
            'status' => $status['status'],
        ];

        $changeSta = Db::table('ad_user_info')->where('id='.$status['id'])->update($sta);

        echo $changeSta;
    }
}